import {defineStore} from "pinia";
import {reactive} from "vue";
import type {MemberLoginForm, MemberLoginRespDataInfo, MemberLoginRespInfo} from "@/type/member.ts";
import {reqMemberInfo, reqMemberLogin, reqMemberLogout} from "@/api/member";

export const useMemberStore = defineStore('member', () => {
    const memberInfo = reactive<MemberLoginRespDataInfo>({
        memberId: Number(localStorage.getItem('member_id')) || -1,
        username: localStorage.getItem('member_username'),
        memberName: localStorage.getItem('member_name'),
        memberType: localStorage.getItem('member_type'),
        avatar: localStorage.getItem('member_avatar'),
        token: localStorage.getItem('member_token')
    })

    // 登录
    async function memberLogin(data: MemberLoginForm) {
        const result: MemberLoginRespInfo = await reqMemberLogin(data)
        if (result.code == 1) {
            memberInfo.memberId = result.data.memberId
            memberInfo.username = result.data.username
            memberInfo.memberName = result.data.memberName
            memberInfo.memberType = result.data.memberType
            memberInfo.avatar = result.data.avatar
            memberInfo.token = result.data.token
            localStorage.setItem('member_id', result.data.memberId.toString())
            localStorage.setItem('member_username', (result.data.username as string))
            localStorage.setItem('member_name', (result.data.memberName as string))
            localStorage.setItem('member_type', (result.data.memberType as string))
            localStorage.setItem('member_avatar', (result.data.avatar as string))
            localStorage.setItem('member_token', (result.data.token as string))
            return 'ok'
        } else {
            return Promise.reject(new Error(result.message))
        }
    }

    // 登出
    async function memberLogout() {
        const result = await reqMemberLogout()
        if (result.code == 1) {
            memberInfo.memberId = -1
            memberInfo.username = ''
            memberInfo.memberName = ''
            memberInfo.memberType = ''
            memberInfo.avatar = ''
            memberInfo.token = ''
            localStorage.removeItem('member_id')
            localStorage.removeItem('member_username')
            localStorage.removeItem('member_name')
            localStorage.removeItem('member_type')
            localStorage.removeItem('member_avatar')
            localStorage.removeItem('member_token')
            return 'ok'
        } else {
            return Promise.reject(new Error(result.code.toString()))
        }
    }

    // 获取会员信息
    async function getMemberInfo(memberId: number) {
        try {
            const result = await reqMemberInfo(memberId);
            if (result.code === 1) {
                // 更新仓库中的 memberInfo
                memberInfo.username = result.data.username;
                memberInfo.memberName = result.data.memberName;
                memberInfo.memberType = result.data.memberType;
                memberInfo.avatar = result.data.avatar;

                // 更新 localStorage
                localStorage.setItem('member_username', result.data.username || '');
                localStorage.setItem('member_name', result.data.memberName || '');
                localStorage.setItem('member_type', result.data.memberType || '');
                localStorage.setItem('member_avatar', result.data.avatar || '');

                // 返回获取到的会员信息
                return result.data;
            } else {
                // 返回错误信息
                return new Error(result.message);
            }
        } catch (error) {
            // 处理异常
            return new Error((error as Error).message);
        }
    }


    return {memberInfo, memberLogin, memberLogout, getMemberInfo}
})